﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */

var JBF = window.JBF = window.jbf = window.$$ = function(selector,context)
{
	if(JBF.Dom)
	{
		//调用DOM操作
		return JBF.Dom(selector,context);
	}
	else
	{
		//未包含DOM操作
		return typeof selector =="string" ? document.getElementById(selector) : selector;
	}
};


//扩展(目标,源)
JBF.apply = function(d,s)
{
    if(d && s && typeof s == 'object')
	{
        for(var p in s)
		{
            d[p] = s[p];
        }
    }
    return d;
};

(function(){
	var idSeed = 0,
	toString = Object.prototype.toString,
	ua = navigator.userAgent.toLowerCase(),
	check = function(r){return r.test(ua);},
	DOC = document,
	isStrict = DOC.compatMode == "CSS1Compat",
	isOpera = check(/opera/),
	isChrome = check(/chrome/),
	isWebKit = check(/webkit/),
	isSafari = !isChrome && check(/safari/),
	isSafari2 = isSafari && check(/applewebkit\/4/), // unique to Safari 2
	isSafari3 = isSafari && check(/version\/3/),
	isSafari4 = isSafari && check(/version\/4/),
	isIE = !isOpera && check(/msie/),
	isIE7 = isIE && check(/msie 7/),
	isIE8 = isIE && check(/msie 8/),
	isIE6 = isIE && !isIE7 && !isIE8,
	isGecko = !isWebKit && check(/gecko/),
	isGecko2 = isGecko && check(/rv:1\.8/),
	isGecko3 = isGecko && check(/rv:1\.9/),
	isFF = isGecko && check(/firefox/),
	isFF2 = isFF && check(/firefox\/2/),
	isFF3 = isFF && check(/firefox\/3/),
	isBorderBox = isIE && !isStrict,
	isWindows = check(/windows|win32/),
	isMac = check(/macintosh|mac os x/),
	isAir = check(/adobeair/),
	isLinux = check(/linux/),
	isSecure = /^https/i.test(window.location.protocol);

	// remove css image flicker
	if(isIE6){
	try{
	DOC.execCommand("BackgroundImageCache", false, true);
	}catch(e){}
	}
	
	//JBF
	JBF.apply(JBF, {
		//版本号
		version : '1.03',
		idSeed : idSeed,
		//空函数
		fn : function(){},
		//空函数
		emptyFn : function(){},
		
		//创建新类(构造方法init)
		createClass : function()
		{
			return function()
			{
				this.init.apply(this, arguments);
			}
		},
		
		//邦定
		bind : function(object, f)
		{
			return function()
			{
				return f.apply(object, arguments);
			}
		},
		
		//列举
		each : function(l, f)
		{
			var i=0, len = l.length, n;
			if(len == undefined)
			{
				for(n in l) f(n, i++, l);
			}
			else
			{
				for( ; i<len ;) f(l[i++], i-1, l);
			}
		},
		//不覆盖扩展
		applyIf : function(d,s)
		{
			if(d && s && typeof s == 'object')
			{
				for(var p in s)
				{
					if(!JBF.isDefined(d[p]))
					{
						d[p] = s[p];
					}
				}
			}
			return d;
		},
		
		//对象长度(字符串中文两字节,英文1个字节)
		len : function(o)
		{
			var i,sum=0;
			if(this.isObject(o))
			{
				if(this.isNumber(o.length))
				{
					return 	o.length;
				}
				else
				{
					this.each(o,function(k,i)
					{
						sum=i;
					});
					return sum+1;
				}
				
			}
			else if(this.isString(o))
			{
				for(i=0;i<o.length;i++)
				{
					if ((o.charCodeAt(i)>=0) && (o.charCodeAt(i)<=255))
					{
						sum=sum+1;
					}
					else
					{
						sum=sum+2;
					}
				}
				return sum;
			}
		},
		
		//转换为数组
		toArray : function()
		{
             return isIE ?
                 function(a, i, j, res)
				 {
                     res = [];
                     for(var x = 0, len = a.length; x < len; x++)
					 {
                         res.push(a[x]);
                     }
                     return res.slice(i || 0, j || res.length);
                 } :
                 function(a, i, j){
                     return Array.prototype.slice.call(a, i || 0, j || a.length);
                 }
         }(),
		
		//json字符串转换为对象
		stringToJson : function(s)
		{
			return eval('('+s+')');
		},
		
		//是否为空
		isNull:function(v)
		{
			return v === null || v === undefined ||  v === '';
		},
		//是否为空
		isEmpty : function(v, allowBlank)
		{
			return v === null || v === undefined || ((JBF.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
		},
		
		//是否是数组
        isArray : function(v)
		{
            return toString.apply(v) === '[object Array]';
        },

        //是否是日期
        isDate : function(v)
		{
            return toString.apply(v) === '[object Date]';
        },
		
		//是否是对象(已过时)
		isObj : function(v)
		{
			return !!v && Object.prototype.toString.call(v) === '[object Object]';
		},
        //是否是对象
        isObject : function(v)
		{
            return !!v && Object.prototype.toString.call(v) === '[object Object]';
        },

        //是否是原始类型(不可再细分的)
        isPrimitive : function(v)
		{
            return JBF.isString(v) || JBF.isNumber(v) || JBF.isBoolean(v);
        },
		
		//是否是函数(已过时)
        isFun : function(v)
		{
            return toString.apply(v) === '[object Function]';
        },
		//是否是函数
        isFn : function(v)
		{
            return toString.apply(v) === '[object Function]';
        },
        //是否是函数
        isFunction : function(v)
		{
            return toString.apply(v) === '[object Function]';
        },

        //是否是数字
        isNum : function(v)
		{
            return typeof v === 'number' && isFinite(v);
        },
		//是否是数字
        isNumber : function(v)
		{
            return typeof v === 'number' && isFinite(v);
        },
		
		//是否是字符串
		isStr : function(v)
		{
			return JBF.isString(v);
		},
        //是否是字符串
        isString : function(v)
		{
            return typeof v === 'string';
        },
		
		//是否是布尔值
		isBool : function(v)
		{
			return JBF.isBoolean(v);
		},
        //是否是布尔值
        isBoolean : function(v)
		{
            return typeof v === 'boolean';
        },
		
		//是否是DOM元素
        isDom : function(v)
		{
            return JBF.isElement(v);
        },
        //是否是DOM元素
        isElement : function(v)
		{
            return !!v && v.tagName;
        },

        //是否定义
        isDefined : function(v)
		{
            return typeof v !== 'undefined';
        },
		
		//是否是电子邮箱
		isEmail:function(v)
		{
			return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(v);
		},
		isReady : false,
		ready : function(handler)
		{
			alert('JBF.Dom未调用,请包含JBF.DOM包!');
			handler();
		},
        //浏览器相关
        isOpera : isOpera,
        isWebKit : isWebKit,
        isChrome : isChrome,
        isSafari : isSafari,
		isSafari2 : isSafari2,
        isSafari3 : isSafari3,
        isSafari4 : isSafari4,
        isIE : isIE,
        isIE6 : isIE6,
        isIE7 : isIE7,
        isIE8 : isIE8,
        isGecko : isGecko,
        isGecko2 : isGecko2,
        isGecko3 : isGecko3,
		isFF : isFF,
		isFF2 : isFF2,
		isFF3 : isFF3,
        isBorderBox : isBorderBox,
        isLinux : isLinux,
        isWindows : isWindows,
        isMac : isMac,
        isAir : isAir
	});
})();

//扩展字符串对象
JBF.applyIf(String, {
	

	//去掉开头和结尾的space, tab, form feed, and line feed (same as [\f\n\r\t\v]).
	trim : function(s)
	{
		return s.replace(/(^\s*)|(\s*$)/g, "");
	},
	//格式化字符串
    format : function(format)
	{
        var args = JBF.toArray(arguments, 1);
        return format.replace(/\{(\d+)\}/g, function(m, i)
		{
            return args[i];
        });
    }
});


/*

String.prototype.Trim = function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.LTrim = function() 
{ 
return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.RTrim = function() 
{ 
return this.replace(/(\s*$)/g, ""); 
} 


*/

//扩展数组对象
JBF.applyIf(Array.prototype, {
    //
    indexOf : function(o, from)
	{
        var len = this.length;
        from = from || 0;
        from += (from < 0) ? len : 0;
        for (; from < len; ++from)
		{
            if(this[from] === o)
			{
                return from;
            }
        }
        return -1;
    },

    //移除对象
    remove : function(o)
	{
       /* var index = this.indexOf(o);
        if(index != -1)
		{
            this.splice(index, 1);
        }
        return this;*/
    }
});



